package org.etherframework.tests.features.mediator
{
	import org.etherframework.core.IModule;
	import org.etherframework.mediator.IMediators;
	import org.etherframework.tests.features.mediator.mediators.LifecycledMediator;
	import org.etherframework.tests.features.mediator.views.TrivialView;
	import org.etherframework.tests.utils.ModuleInfo;
	import org.etherframework.tests.utils.TestUtility;
	import org.flexunit.async.Async;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.assertThat;
	import org.hamcrest.collection.array;
	
	import spark.components.Group;

	public class when_using_mediator_lifecycle
	{	
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _module:IModule;
		private static var _lifecycledMediator:LifecycledMediator;
		private static var _m:IMediators;
		private static var _moduleView:Group;
		private static var _moduleNestedView:Group;
		private static var _info:ModuleInfo;
		private static var _view:TrivialView;
		private static var _pendentView:TrivialView;
		private static var _pendentMediator:LifecycledMediator;
		
		[BeforeClass(async)]
		public static function setUpBeforeClass():void
		{
			_moduleView = UIImpersonator.addChild(new Group()) as Group;
			_info = _testUtility.createModuleInfo(null, _moduleView);
			_moduleNestedView = new Group();
			_module = _info.module;
			_m = _module.mediators;
			_view = new TrivialView();
			_pendentView = new TrivialView();
			
			_m.addMediator(TrivialView, LifecycledMediator);
			
			_moduleView.addElement(_view);
			_lifecycledMediator = _m.hasMediator(_view) as LifecycledMediator;
			
			_moduleNestedView.addElement(_pendentView);
			_pendentMediator = _m.createMediator(_pendentView) as LifecycledMediator;
			
			Async.delayCall(when_using_mediator_lifecycle, function():void{}, 100);
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			_testUtility.dispose();
			UIImpersonator.removeAllChildren();
		}
		
		[Test(order=1)]
		public function mediator_lifecycle_should_be_executed():void
		{
			assertThat(_lifecycledMediator.lifecycleOrder, 
				array(LifecycledMediator.PERSISTED, LifecycledMediator.INITIATE, LifecycledMediator.COMPLETE));
		}
		
		[Test(order=2,async)]
		public function mediator_lifecycle_should_be_executed_when_created_manually():void
		{
			const view:TrivialView = new TrivialView();
			const mediator:LifecycledMediator = _m.createMediator(view) as LifecycledMediator;
			_moduleView.addElement(view);
			
			Async.delayCall(this, function():void
			{
				assertThat(mediator.lifecycleOrder, 
					array(LifecycledMediator.PERSISTED, LifecycledMediator.INITIATE, LifecycledMediator.COMPLETE));
			}, 100);
		}
		
		[Test(order=3,async)]
		public function mediator_lifecycle_should_be_executed_with_pendent_view():void
		{
			_moduleView.addElement(_pendentView);
			
			Async.delayCall(this, function():void
			{
				assertThat(_pendentMediator.lifecycleOrder, 
					array(LifecycledMediator.PERSISTED, LifecycledMediator.INITIATE, LifecycledMediator.COMPLETE));
			}, 100);
		}
	}
}